from django.shortcuts import render, HttpResponse
from .models import HisArticle, Scheme, WebDistribute
import datetime
from django.http.response import JsonResponse


def index(request):
    dict = request.GET
    # print(dict)
    if len(dict) == 0:
        articles = HisArticle.objects.all()[0:5]
        # print(dict)
    else:
        time = dict.get('time')
        # print(type(time))
        if time == '1':
            articles = HisArticle.objects.filter(pub_time=datetime.date.today())
        elif time == '2':
            start = datetime.datetime.now()-datetime.timedelta(days=7)
            articles = HisArticle.objects.filter(pub_time__gt=start)
        elif time == '3':
            start = datetime.datetime.now()-datetime.timedelta(days=30)
            articles = HisArticle.objects.filter(pub_time__gt=start)
        else:
            articles = HisArticle.objects
        sentiment = dict.get('sentiment')
        if sentiment == '1':
            articles = articles.filter(sentiment=1)
        elif sentiment == '2':
            articles = articles.filter(sentiment=2)
        elif sentiment == '3':
            articles = articles.filter(sentiment=3)
        # print(articles)
        source = dict.get('source')
        if source == '1':
            articles = articles.filter(website='微博')
        elif source == '2':
            articles = articles.filter(website='微信')
        elif source == '3':
            articles = articles.filter(website='知乎')
        elif source == '4':
            articles = articles.filter(website='网站')
    # HisArticle.objects.get(sentiment=1)
    # HisArticle.objects.filter(sentiment=1)
    count = articles.count()
    if count == 0:
        return render(request, 'News/index.html',)
    elif count < 6:
        return render(request, 'News/index.html', {'articles': articles})
    else:
        return render(request, 'News/index.html', {'articles': articles[0:5]})

# Create your views here.


def index_ajax(request):
    return render(request, 'News/indexAjax.html')


def ajax(request):
    dic = request.GET
    source_dic = {
        '1': '微博',
        '2': '微信',
        '3': '知乎',
        '4': '网站',
    }
    sentiment_dic = {
        '1': 1,
        '2': 2,
        '3': 0,
    }
    topic_dic = {
        '1': '中国男篮 夏季联赛',
        '2': '河北 财政 河北财政厅',
    }
    time = dic.get('time')
    if time == '1':
        articles = HisArticle.objects.filter(pub_time=datetime.date.today())
    elif time == '2':
        start = datetime.datetime.now() - datetime.timedelta(days=7)
        articles = HisArticle.objects.filter(pub_time__gt=start)
    elif time == '3':
        start = datetime.datetime.now() - datetime.timedelta(days=30)
        articles = HisArticle.objects.filter(pub_time__gt=start)
    else:
        articles = HisArticle.objects
    sentiment = dic.get('sentiment')
    if sentiment != '0':
        articles = articles.filter(sentiment=sentiment_dic.get(sentiment))
    source = dic.get('source')
    if source != '0':
        articles = articles.filter(website=source_dic.get(source))
    topic = dic.get('topic')
    if topic != '0':
        articles = articles.filter(keyword=topic_dic.get(topic))
    # print(articles)
    count = articles.count()
    # if count == 0:
    #     pass
    # elif count < 6:
    #     art
    # else:
    #     pass
    articles = articles[0:5]
    articles_dic = {}
    for ind, article in enumerate(articles):
        temp = {}
        temp['title'] = article.title
        temp['author'] = article.author
        temp['website'] = article.website
        temp['url'] = article.url
        temp['pub_time'] = article.pub_time
        # print(article.pub_time)
        temp['abstract'] = article.abstract
        temp['sentiment'] = article.sentiment
        temp['top_word'] = article.top_word
        articles_dic[ind] = temp
    return JsonResponse(articles_dic)
# def index_time(request, time):
#     return HttpResponse(time)


def analysis(request):

    return render(request, 'News/analysis.html')


def analysis_ajax(request):
    dic = request.GET
    topic = dic.get('topic')
    print(topic)
    top_dic = {
        1: '中国男篮 夏季联赛',
        2: '河北 财政 河北财政厅',
    }
    websites = WebDistribute.objects.filter(keyword=top_dic.get(topic))
    data = {
        'name': [],
        'data': [],
    }
    for website in websites:
        data['name'].append(website.website)
        data['data'].append({website.website: website.count})
    print("11111111111")
    return JsonResponse(data)

